import React, { Dispatch } from 'react';
import { AnnounceManageAPI } from './type';

/** 定义管理页面上下文 */
export interface IManageContext {
  dataSource: Record<string, any>[];
  loading: boolean;
  refreshRequest: (requestParams: AnnounceManageAPI.ITableRequestType) => any;
  dicMapList: Record<string, any>;
  pagination: Record<string, any>;
  requestParams: Record<string, any>;
}

/** 初始化管理页面上下文 */
const init_manage_context: IManageContext = {
  dataSource: [],
  loading: false,
  refreshRequest: ({}) => {},
  dicMapList: [],
  pagination: {},
  requestParams: {},
};

/** 创建公告管理上下文 */
const AnnounceManageContext = React.createContext(init_manage_context);

/**
 * 定义新增和编辑上下文
 * @prefixCls 标签标记前缀
 * @dispatch 派发匹配
 * @curRes 当前资源
 * @leftSearchValue 左侧树搜索值
 */
export interface IUpdateContext {
  prefixCls: string;
  dispatch: Dispatch<any>;
  curRes: Record<string, any>;
  leftSelectRows: Record<string, any>[];
  leftSelectKeys: string[];
  leftExpandedKeys: string[];
  leftAutoExpandParent: boolean;
  announceInfo: Record<string, any>;
  reloadAnnounceInfo: () => void;
  locationProps: Record<string, any>;
}

/** 初始化新增和编辑上下文 */
const init_update_context: IUpdateContext = {
  prefixCls: 'announce-',
  dispatch: () => {},
  curRes: {},
  leftSelectRows: [],
  leftSelectKeys: [],
  leftExpandedKeys: [],
  leftAutoExpandParent: false,
  announceInfo: {},
  reloadAnnounceInfo: () => {},
  locationProps: {},
};

/** 创建公告新增和编辑上下文 */
const AnnounceUpdateContext = React.createContext<IUpdateContext>(
  init_update_context,
);

/**
 * 公告详情上下文
 */
export interface IDetailContext {
  prefixCls: string;
  curRes: Record<string, any>;
  announceInfo: Record<string, any>;
  reloadAnnounceInfo: () => void;
  locationProps: Record<string, any>;
  announceLoading: boolean;
}

/** 初始化公告详情上下文 */
const init_detail_context: IDetailContext = {
  prefixCls: 'announce-update-',
  curRes: {},
  announceInfo: {},
  reloadAnnounceInfo: () => {},
  locationProps: {},
  announceLoading: false,
};

/** 创建公告新增和编辑上下文 */
const AnnounceDetailContext = React.createContext<IDetailContext>(
  init_detail_context,
);

/** 导出上下文 */
export { AnnounceManageContext, AnnounceUpdateContext, AnnounceDetailContext };
